/*888geoserverAPI
 * @Author: dlfan
 * @Date: 2022-10-20 16:00:41
 * @LastEditors: dlfan
 * @LastEditTime: 2022-11-04 21:18:12
 * @FilePath: \ufish\src\server\api.js
 * @Description: 所有的 service 请求入口
 * 
 * Copyright (c) 2022 by glut dlfan, All Rights Reserved. 
 */
/* eslint-disable */
import axios from 'axios';
import { ProServerMetaInfo, DevServerMetaInfo } from '@/config/config.js';

// 服务器URI
const base = ProServerMetaInfo.baseAPI
// const base = DevServerMetaInfo.baseAPI

const _axios = axios.create()
_axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token')
        if (token) {
            config.headers['token'] = token
        }
        return config
    },
    err => {
        console.log(err)
    }
)
_axios.interceptors.response.use(res => {
    if (res.status && res.status == 200 && res.data.code != 200) {
        const token = localStorage.getItem('token');
        if ((res.data.code === 5110 || res.data.code === 5109) && token) {
            //   localStorage.removeItem('token');
            //   store.commit("tokenExpiring");
        }
    }
    const result = responseUnifiedProcessing(res);
    return result
}, err => {
    return { isOk: false, msg: err.message};
})


//  自定义请求
export const customRequest = (config) => {
    return _axios(config)
}

export const postRequestJson = (url, params) => {
    return _axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8;'
        }
    })
}

export const postRequest = (url, params) => {
    return _axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
}
export const uploadFileRequest = (url, params) => {
    return _axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
export const putRequest = (url, params) => {
    return _axios({
        method: 'put',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
}
export const deleteRequest = (url) => {
    return _axios({
        method: 'delete',
        url: `${base}${url}`
    })
}
export const getRequest = (url) => {
    return _axios({
        method: 'get',
        url: `${base}${url}`
    })
}

export function responseUnifiedProcessing(res) {
    const result = { isOk: false, msg: '', data: null };
    if (res != undefined && res.status == 200) {
        result.isOk = true;
        result.data = res.data.data ? res.data.data : res.data.results ;
        
    } else {
        result.msg = res.data ? res.data.msg : res.msg;
    }
    return result;
}

export function createUrl(url, params) {
    const paramarr = Object.entries(params);
    url += '?';
    paramarr.forEach(param => {
        url += param[0] + '=' + param[1] + '&';
    });
    return url.slice(0, url.length - 1);
}

export const optionRequestToOceanImg = (url) => {
    return _axios({
        method: 'HEAD',
        url: `${url}`
    })
}
